Here, we will introduce the concept of iteration structure derived from structured programming.
Let's briefly explain the evolution of high-level languages. Starting with unstructured programming, structured programming, and modularization, we finally arrive at object-oriented programming, which is the focus of this series. Different languages have developed their own characteristics and have inherited some concepts from the previous design. For example, the conditional judgment we learned in the previous chapter belongs to the selection structure.
A repetitive structure can also be called a loop. It mainly refers to the continuous execution of a block of program code until a certain condition is met through the determination of special conditions, and then the loop is exited. The following introduces three loop methods that can be used in different situations.
//for( initial value ; condition ; control elememt)
int number;
for (number = 0 ; number < 5 ; number++){
System.out.print(number + " "); //Outputs : 0 1 2 3 4
}
→ Program analysis: The for loop requires a total of three conditions to be set manually: initial value, condition judgment, and control increment/decrement. In this program, we set it to start from 0 (initial value), increase by one unit each time (increment/decrement), and exit when the value is greater than or equal to 5, otherwise it will continue to execute (condition judgment). When the program number = 0, it enters the execution program and prints 0. When it enters the next time, it will first increase by one unit and then determine whether the condition is met. It will only enter the loop when the condition is met. Until the end of the loop when number = 4, it will increase by one unit to 5, which does not meet the condition, so it will leave the loop.
public class Forloop{
public static void main(String[] args){
int max=0, i, temp;
for( i=1 ; i<=5 ; i++){
temp = (int)(Math.random() * 101 );
System.out.println(temp);
if(temp > max)
max = temp ;
}
System.out.println("Maximum = " + max);
}
}
/*for (variableType variableName : arrayName)*/
String[] name={"Jason","Johnny","Johnson"};
for(String nameNew : name){
System.out.println(nameNew);
}
In short, it is to create a concept similar to an index by looping through the contents of an array and printing them one by one. It's okay if you don't understand it here, the concept of arrays will be explained separately in a later chapter.
/*while(condition){
.................
control element;
}*/
int sum = 0;
int add = 1;
while (sum < 100){
sum += add;
add += 1;
}
System.out.println(sum);
→Program analysis: We want to know which number is closest to 100 by starting with 1 and adding together the numbers (1+2+3+…). Because we don't know how many times we have to add the numbers to get the one closest to 100, we use a while loop that only executes the loop when the total is less than 100. The block of code calculates the result after each addition, and the condition is judged once before each entry.
/*do{
code to be executed;
}while(condition);*/
int num = 10;
do{
System.out.print(num + " "); //Outputs : 10 9 8 7 6
num--;
}while(num > 5)
System.out.println();
System.out.println(num); //Outputs: 5
→Program analysis: We set the initial value to 10 and use do-while to print out all integers greater than 5. The key points of this program are lines 7, 8, and 10, because after each number is printed, it is subtracted by 1 before the conditional judgment is made. When we print 6, after subtracting 1, it does not meet the condition of being greater than 5, so it jumps out. At this time, num stores 5 after being subtracted by 1, and line 10 will print 5.
***! Special note! *** The do-while loop ends after the last while loop, and a ;
is added.
A comparison of the three cycles is shown below:
此圖參考C語言學習手冊-第四版
The function of the song list at the end of the chapter topic “Day 8 Conditional Expressions” is incorrect. The following is the correction:
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int function;
while(true){
System.out.println("請輸入功能編號: ");
function = input.nextInt();
switch(function){
case 1 :
System.out.println("歌手查詢");
break;
case 2 :
System.out.println("歌名查詢");
break;
case 3 :
System.out.println("更改資訊");
break;
case 4 :
System.out.println("隨機撥放");
break;
default :
System.out.println("輸入錯誤");
break;
}
}
}
}